// pages/openid/openid.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasUserInfo: false,
    userInfo: null,
    rawData: '',
    signature: '',
    code: '',
    jwtMessage:'',
  },
  userAuthorized() {
    var that=this;
    wx.showLoading({
      title: '请稍等',
    });
    wx.getSetting({
      success: data => {
        if (data.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: data => {
              wx.setStorageSync('userInfo', data.userInfo)
              console.log(data);
              this.setData({
                hasUserInfo: true,
                userInfo: data.userInfo,
                rawData: data.rawData,
                signature: data.signature
              })
              wx.login({
                success: function (res) {
                  if (res.code) {
                    //发起网络请求
                    wx.request({
                      url: "http://localhost:8080/wechat/login",
                      method: "POST",
                      header: {
                        "content-type": "application/x-www-form-urlencoded"
                      },
                      data: {
                        'code': res.code,
                        'rawData': that.data.rawData,
                        'signature': that.data.signature,
                      },
                      success:function(res){
                        wx.hideLoading();
                        console.info(res);
                        if (res.statusCode == 200){
                          if(res.data['status']=='fail'){
                            wx.showToast({
                              title: res.data['jwtMessage'],
                              icon: 'none',
                              duration: 2000
                            })
                          }else{
                            wx.removeStorageSync('Authorization');
                            wx.setStorageSync('Authorization', res.data['Authorization']);
                            wx.switchTab({
                              url: '/pages/index/index',
                            })
                            console.log(res);
                          }
                        }else{
                          wx.showToast({
                            title: '登录失败',
                            icon: 'none',
                            duration: 2000
                          })
                        }
                      }
                    })
                  } else {
                    console.log('获取用户登录态失败！' + res.errMsg)
                  }
                }
              })
            }
          })
        } else {
          this.setData({
            hasUserInfo: false
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var jwtMessage=wx.getStorageSync('jwtMessage');
    if(jwtMessage!=''){
      wx.showToast({
        title: jwtMessage,
        icon: 'none',
        duration: 2000
      })
      wx.removeStorageSync('jwtMessage');
    }
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})